package com.fzy.common.tree;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * tree实体
 *
 * @author 冯赵杨
 * @create 2018/9/27
 */
public class Tree {
    private String id;
    private String text;
    private String url;
    private String checked;
    private String path;
    private Tree parent;
    private List<Tree> childs = new ArrayList<Tree>();

    public Tree() {
    }


    public Tree(String id, String text, String path, String url, String checked, Tree parent) {
        this.id = id;
        this.text = text;
        if (url == null) {
            this.url = "";
        } else {
            this.url = url;
        }
        this.path = path;
        this.checked = checked;
        this.parent = parent;
        if (this.parent != null) {
            this.parent.addChild(this);
        }
    }

    public boolean addChild(Tree Tree) {
        return this.childs.add(Tree);
    }

    public Iterator<Tree> getChildIterator() {
        return childs.iterator();
    }

    public List<Tree> getChilds() {
        return this.childs;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Tree getParent() {
        return parent;
    }

    public void setParent(Tree parent) {
        this.parent = parent;
    }

    public int level() {
        int result = 0;
        while (this.parent != null) {
            result++;
        }
        return result;
    }

    public boolean hasChilds() {
        return !this.childs.isEmpty();
    }

    public Tree getFirstLeaf() {
        if (this.hasChilds()) {
            return this.childs.get(0).getFirstLeaf();
        }
        return this;
    }

    public Tree getTopParent() {
        if (this.parent == null) {
            return this;
        } else {
            return this.parent.getTopParent();
        }
    }

    public List<Tree> getTreePath() {
        List<Tree> list = new ArrayList<Tree>();
        Tree tmp = this.getParent();
        while (tmp != null) {
            list.add(tmp);
            tmp = tmp.getParent();
        }
        return list;
    }

    public String getStringPath() {
        StringBuffer sb = new StringBuffer();
        List<Tree> tmp = this.getTreePath();
        for (int i = tmp.size() - 1; i >= 0; i--) {
            Tree Tree = tmp.get(i);
            sb.append(Tree.getText() + ".");
        }
        sb.append(this.getText());
        return sb.toString();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getChecked() {
        return checked;
    }

    public void setChecked(String checked) {
        this.checked = checked;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

}
